package com.ruoyi.web.controller.system;

import java.io.File;
import java.net.MalformedURLException;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.VehicleUsage;
import com.ruoyi.system.service.IVehicleUsageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆使用记录Controller
 * 
 * @author ruoyi
 * @date 2025-04-05
 */
@Controller
@RequestMapping("/system/vehicleUsage")
public class VehicleUsageController extends BaseController
{
    private String prefix = "system/vehicleUsage";

    @Autowired
    private IVehicleUsageService vehicleUsageService;

    @RequiresPermissions("system:vehicleUsage:view")
    @GetMapping()
    public String vehicleUsage()
    {
        return prefix + "/vehicleUsage";
    }

    /**
     * 查询车辆使用记录列表
     */
    @RequiresPermissions("system:vehicleUsage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VehicleUsage vehicleUsage)
    {
        startPage();

        List<VehicleUsage> list = vehicleUsageService.selectVehicleUsageList(vehicleUsage);
        for (VehicleUsage vehicleUsageItem : list) {
            vehicleUsageItem.setStatusName(vehicleUsageItem.transStatusName());
        }
        return getDataTable(list);
    }

    /**
     * 导出车辆使用记录列表
     */
    @RequiresPermissions("system:vehicleUsage:export")
    @Log(title = "车辆使用记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VehicleUsage vehicleUsage)
    {
        List<VehicleUsage> list = vehicleUsageService.selectVehicleUsageList(vehicleUsage);
        for (VehicleUsage vehicleUsageItem : list) {
            vehicleUsageItem.setStatusName(vehicleUsageItem.transStatusName());
        }
        ExcelUtil<VehicleUsage> util = new ExcelUtil<VehicleUsage>(VehicleUsage.class);
        return util.exportExcel(list, "车辆使用");
    }

    /**
     * 新增车辆使用记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆使用记录
     */
    @RequiresPermissions("system:vehicleUsage:add")
    @Log(title = "车辆使用记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VehicleUsage vehicleUsage)
    {
        return toAjax(vehicleUsageService.insertVehicleUsage(vehicleUsage));
    }

    /**
     * 修改车辆使用记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        VehicleUsage vehicleUsage = vehicleUsageService.selectVehicleUsageById(id);
        mmap.put("vehicleUsage", vehicleUsage);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆使用记录
     */
    @RequiresPermissions("system:vehicleUsage:edit")
    @Log(title = "车辆使用记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VehicleUsage vehicleUsage)
    {
        return toAjax(vehicleUsageService.updateVehicleUsage(vehicleUsage));
    }

    /**
     * 删除车辆使用记录
     */
    @RequiresPermissions("system:vehicleUsage:remove")
    @Log(title = "车辆使用记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(vehicleUsageService.deleteVehicleUsageByIds(ids));
    }

    /**
     * 归还车辆
     */
    @RequiresPermissions("system:vehicleUsage:retrunCar")
    @Log(title = "归还车辆", businessType = BusinessType.UPDATE)
    @PostMapping("/returnCar")
    @ResponseBody
    public AjaxResult retrunCar(VehicleUsage vehicleUsage)
    {
        return toAjax(vehicleUsageService.retrunCar(vehicleUsage));
    }
}
